#ifndef SMART_PWM_H
#define SMART_PWM_H
#include "typedefs.h"
/*
    1 / 2000000 = 0.0000005s;500ns/bit
    29个灯，总共124个字节 = 0.496ms   200个灯，总共808个字节 = 3.232ms
*/
/*###############################有这个标识表示可以修改的内容*/

#define LED_NUMBER  29U                     /*灯数量 需要根据具体灯珠数量修改###############################*/
#define NUMBER_LIGHT_BYTES (8u+(LED_NUMBER*4u))

#define CRITICAL_BEAD_POSITION  (LED_NUMBER/2)  /*解锁上锁功能临界灯珠位置*/
#define LOCK_NUMBER_LIGHTS_MAX  10u             /*解锁上锁功能情况下流水的最多灯珠数量 根据需求修改###############################*/
#define LOCK_GRADIENT_NUMBER    3u              /*解锁上锁功能情况下流水的亮度渐变灯珠数量,单边单侧的数量 根据需求修改###############################*/
#define FINAL_NUMBER_LIGHTS     (LOCK_GRADIENT_NUMBER)              /*解锁上锁功能情况下第三阶段最后亮灯数量*/

#if (0 != LED_NUMBER%2)/*灯珠总数为单数*/
#define UNLOCK_LED_RIGHT_ST ((CRITICAL_BEAD_POSITION&0xff)+1) /*解锁模式下灯珠第一阶段中间向右状态的起始灯珠位置*/
#else/*灯珠总数为双数*/
#define UNLOCK_LED_RIGHT_ST (CRITICAL_BEAD_POSITION&0xff)     /*解锁模式下灯珠第一阶段中间向右状态的起始灯珠位置*/
#endif

#define BRIGHTNESS_VARIATION_VALUE  (0x0Au) /*亮度单次变化值###############################*/
#define DIM_MAX_VALUE               (0x1Fu) /*亮度最大亮度上限 0x1Fu###############################*/
#define DIM_CLOSE                   (0x0)   /*熄灭灯*/

#define CHARGING_CHANGES_NUMBER             10u    /*充电规律变化组的总LED颗数###############################*/
#define CHARGING_CHANGES_LIGHT_NUMBER       3u    /*充电规律变化组中的两边灯亮度会变化的单边LED数目###############################*/
#define CHARGING_CHANGES_NO_LIGHT_NUMBER    (CHARGING_CHANGES_NUMBER-(2*CHARGING_CHANGES_LIGHT_NUMBER))    /*充电规律变化组中的中间灯亮度不会变化的单边LED数目*/





/*颜色dome_PWM值：白色(255,255,255)、蓝色(0,0,255)、黄色(255,255,0)、红色(255,0,0)、绿色(0,255,0)*/
#define COLOR_WHITE_PWM_R 255U  /*白色*/
#define COLOR_WHITE_PWM_G 255U
#define COLOR_WHITE_PWM_B 255U

#define COLOR_BLUE_PWM_R 0U     /*蓝色*/
#define COLOR_BLUE_PWM_G 0U
#define COLOR_BLUE_PWM_B 255U

#define COLOR_YELLOW_PWM_R 255U /*黄色*/
#define COLOR_YELLOW_PWM_G 255U
#define COLOR_YELLOW_PWM_B 0U

#define COLOR_RED_PWM_R 255U    /*红色*/
#define COLOR_RED_PWM_G 0U
#define COLOR_RED_PWM_B 0U

#define COLOR_GREEN_PWM_R 0U    /*绿色*/
#define COLOR_GREEN_PWM_G 255U
#define COLOR_GREEN_PWM_B 0U

#define RGB_PWM_CLOSE 0U  /*关闭颜色*/

#define STARTING_SIGNAL_ALL_0 (0u)          /*起始信号全0*/
#define END_SIGNAL_ALL_1      (0xFFFFFFFFu) /*结束信号全1*/
#define LED_FLAG_BIT          (0x7u)        /*每个灯的flag标志*/

/*
1.OBU发出车辆解锁信号，依次显示白色和蓝色的欢迎动画，由内向两侧示阔，进行两次示阔。
2.OBU发出车辆上锁信号，显示黄色再见动画，由两侧外向内收缩。
3.OBU发出车辆转向信号，左转；向左进行流动，右转：向右流动，循环10次。（蓝色）
4.OBU发出车辆预警信号，红色灯光闪烁。收到预警解除信号后，停止闪烁。
5.OBU发出车辆充电信号，绿色灯光进行堆栈模式，模拟手机充电显示。
*/
typedef enum 
{
    LIGHT_EFFECT_SIGNAL_MAX,    /*初始状态：当前无信号需要处理*/
    VEHICLE_UNLOCKING,          /*OBU发出车辆解锁信号*/
    VEHICLE_LOCKING,            /*OBU发出车辆上锁信号*/
    VEHICLE_TURNING_LEFT,       /*OBU发出车辆左转信号*/
    VEHICLE_TURNING_RIGHT,      /*OBU发出车辆右转信号*/
    VEHICLE_WARNING,            /*OBU发出预警信号*/
    VEHICLE_RELEASE_WARNING,    /*OBU发出解除预警信号*/
    VEHICLE_CHARGING            /*OBU发出充电信号*/
}Light_effect;

typedef enum 
{
    BRIGHTNESS_CONTROL_SIGNAL_MAX,  /*初始状态：当前无信号*/
    BRIGHTNESS_DECREASE,            /*亮度减小*/
    BRIGHTNESS_INCREASE,            /*亮度增加*/
    BRIGHTNESS_CLOSE,               /*亮度关闭*/
    BRIGHTNESS_OPEN,                /*亮度开启*/
    BRIGHTNESS_REMAINS_UNCHANGED    /*亮度不变*/
}Brightness_control;

typedef enum
{
    NO_STAGE,
    ONE_PHASE,
    TWO_PHASE,
    THREE_PHASE
}light_stage;/*灯光控制阶段*/

typedef struct
{
    light_stage light_stage_unLock;           /*灯光阶段*/
    uint8_t Number_cycles;                  /*循环次数*/

    uint8_t Middle_Left;                    /*中间向左方向扩展的顶灯数量（《《《《《中间）*/
    uint8_t Middle_Left_num;                /*中间向左方向三阶段的灯珠数量*/

    uint8_t Middle_Right;                   /*中间向右方向扩展的顶灯数量（中间》》》》》）*/ 
    uint8_t Middle_Right_num;               /*中间向右方向三阶段的灯珠数量*/
    
}UnLock_control;/*车辆解锁功能控制结构体*/

typedef struct
{
    light_stage light_stage_Lock;           /*灯光阶段*/
    uint8_t Number_cycles;                  /*循环次数*/

    uint8_t Right_retract_ceiling_light;    /*向右方向收缩的顶灯 （》》》》》中间）*/
    uint8_t Right_Three_Phase_light_num;    /*向右方向三阶段的灯珠数量*/

    uint8_t Left_retract_ceiling_light;     /*向 左方向收缩的顶灯 （中间《《《《《）*/
    uint8_t Left_Three_Phase_light_num;     /*向 左方向三阶段的灯珠数量*/
    
}Lock_control;/*车辆上锁功能控制结构体*/

typedef struct
{
    uint8_t Right_Number;
    uint8_t Left_Number;
}Left_Right;/*左右转向灯效次数结构体*/

typedef struct
{
    uint8_t Battery_Percentage;             /*电量百分比*/
    uint8_t Led_Battery_offset;             /*LED电量偏移灯珠临界位置*/
    uint8_t Light_led_num;                  /*当前点亮的LED为规律灯效中的哪一颗*/
}Charging_control;                          /*充电模式控制结构体*/


typedef struct
{
    uint8_t B_PWM;       /*计算出来的Blue-PWM值*/
    uint8_t G_PWM;       /*计算出来的Green-PWM值*/
    uint8_t R_PWM;       /*计算出来的Red-PWM值*/
}Led_pwm;

typedef struct
{
    uint8_t Flag_bit : 3;   /*标志位*/
    uint8_t Dim : 5;        /*亮度上限调节位*/
    uint8_t Blue_PWM;       /*每个灯不同的Blue  PWM调节位*/
    uint8_t Green_PWM;      /*每个灯不同的Green PWM调节位*/
    uint8_t Red_PWM;        /*每个灯不同的Red   PWM调节位*/
}single_led_data;

typedef union
{
    struct
    {
        uint32_t Start_signal;                  /*起始信号全0*/
        single_led_data Smt_led[LED_NUMBER];    /*全部每个LED的data*/  
        uint32_t End_signal;                    /*结束信号全1*/
    } sig;
    uint8_t Smart_control_data[NUMBER_LIGHT_BYTES];
}whole_led_data;

typedef struct
{
    Light_effect        Light_effect_now;       /*LED控制功能*/
    Brightness_control  Brightness_control_now; /*LED控制亮度*/
    Led_pwm             Led_pwm_now;            /*LED当前RGB-PWM颜色数值->拿去当灯条的起始颜色*/
    uint8_t             Led_Dim_upperlimit_now; /*LED当前亮度上限，只用5个bit*/
    whole_led_data      whole_led_data_now;     /*LED灯条控制数据*/
}led_control_information; /*灯控制结构体*/

void led_control_init(void);
void Clear_structural_data(void);
uint8_t Vehicle_Unlock_func_set_smart_pwm(void);
uint8_t Vehicle_lock_func_set_smart_pwm(void);
uint8_t Vehicle_turning_left_func_set_smart_pwm(void);
uint8_t Vehicle_turning_right_func_set_smart_pwm(void);
void Vehicle_warning_func_set_smart_pwm(void);
void charging_set_Battery(uint8_t Battery);
void Vehicle_charging_func_set_smart_pwm(void);
void close_smart_pwm(void);
void send_smart(void);
void st_Light_effect_now(Light_effect effect);
Light_effect rt_Light_effect_now(void);
void st_Brightness_control_now(Brightness_control control_now);
Brightness_control rt_Brightness_control_now(void);
void st_Led_Dim_upperlimit_now(uint8_t Dim_upperlimit);
uint8_t rt_Led_Dim_upperlimit_now(void);
void st_Led_pwm_now(Led_pwm led_pwm_st);
Led_pwm rt_Led_pwm_now(void);
void led_control_test(void);
#endif
